/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.biz.gen.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.smooth.common.site.common.config.Global;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.StringUtils;
import com.smooth.common.site.common.web.BaseController;
import com.smooth.dao.gen.BizGentestMapper;
import com.smooth.dao.gen.bean.BizGentest;
import com.smooth.site.sx.mag.biz.gen.service.BizGentestService;

/**
 * gentestController
 * @author 北冥
 * @version 2018-12-11
 */
@Controller
@RequestMapping(value = "${adminPath}/gen/bizGentest")
public class BizGentestController extends BaseController {

	@Autowired
	private BizGentestService bizGentestService;
	
	@ModelAttribute
	public BizGentest get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BizGentest entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizGentestService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BizGentest();
		}
		return entity;
	}
	
	@RequiresPermissions("gen:bizGentest:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizGentest bizGentest, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizGentest> page = bizGentestService.findPage(new Page<BizGentest>(request, response), bizGentest); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/gen/bizGentestList";
		}
		return "biz/gen/bizGentestList";
	}
	
	@RequiresPermissions("gen:bizGentest:view")
	@RequestMapping(value = "form")
	public String form(BizGentest bizGentest, Model model) {
		model.addAttribute("bizGentest", bizGentest);
		return "biz/gen/bizGentestForm";
	}

	@RequiresPermissions("gen:bizGentest:edit")
	@RequestMapping(value = "save")
	public String save(BizGentest bizGentest, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizGentest)){
			return form(bizGentest, model);
		}
		try{
			bizGentestService.save(bizGentest);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/gen/bizGentest?repage";
		}
		addMessage(redirectAttributes, "保存gentest成功");
		return "redirect:"+Global.getAdminPath()+"/gen/bizGentest?repage";
	}
	
	@RequiresPermissions("gen:bizGentest:edit")
	@RequestMapping(value = "delete")
	public String delete(BizGentest bizGentest, RedirectAttributes redirectAttributes) {
		try{
			bizGentestService.delete(bizGentest);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/gen/bizGentest?repage";
		}
		addMessage(redirectAttributes, "删除gentest成功");
		return "redirect:"+Global.getAdminPath()+"/gen/bizGentest?repage";
	}

}